package net.begincode.biz.impl;

import com.google.common.collect.Lists;
import net.begincode.bean.BlogClassInfo;
import net.begincode.bean.BlogDetaiInfo;
import net.begincode.biz.BlogBiz;
import net.begincode.blog.bean.param.BlogClassQueryParam;
import net.begincode.blog.bean.param.BlogQueryParam;
import net.begincode.blog.bean.vo.BlogBaseInfo;
import net.begincode.blog.bean.vo.BlogWithKeyWords;
import net.begincode.blog.bean.vo.CheckBlog;
import net.begincode.blog.bean.vo.UserHonorInfo;
import net.begincode.blog.entity.Blog;
import net.begincode.blog.service.BlogService;
import net.begincode.blog.service.UserService;
import net.begincode.convert.BlogConvertUtils;
import net.begincode.page.PageResponse;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Component
public class BlogBizImpl implements BlogBiz {
    @Resource
    private BlogService blogService;
    @Resource
    private UserService userService;

    @Override
    public int setBlogKeyWords(Long blogId, List<Long> keyWordIds) {
        return blogService.addBlogKeyWords(blogId, keyWordIds);
    }

    @Override
    public int delBlogKeyWords(Long blogId, List<Long> keyWordIds) {
        return blogService.delBlogKeyWords(blogId, keyWordIds);
    }

    @Override
    public int setBlogHomeType(Long blogId, Long homeTypeId) {
        return blogService.addOrUpdateHomeTypeBlog(blogId,homeTypeId);
    }

    @Override
    public int updateBlogById(Blog blog) {
        return blogService.updateBlog(blog);
    }

    @Override
    public PageResponse<CheckBlog> queryCheckBlog(BlogQueryParam queryParam) {
        return blogService.selectCheckBlog(queryParam);
    }

    @Override
    public BlogDetaiInfo queryBlogById(Long blogId, String blogFlag) {
        BlogWithKeyWords blogWithKeyWords =  blogService.queryBlogById(blogId,blogFlag);
        if(Objects.isNull(blogWithKeyWords)){
            return null;
        }
        BlogDetaiInfo blogDetaiInfo = BlogConvertUtils.convert(blogWithKeyWords.getBlog(), blogWithKeyWords.getKeyWords());
        UserHonorInfo userHonorInfo = userService.queryUserHonor(blogDetaiInfo.getUserId());
        blogDetaiInfo.setUserHonorInfo(userHonorInfo);
        return blogDetaiInfo;
    }

    @Override
    public PageResponse<BlogClassInfo> queryClassByQuery(BlogClassQueryParam queryParam) {
        int totalCount = blogService.countClass(queryParam);
        if(totalCount == 0){
            return PageResponse.success(Lists.newArrayList(),queryParam.getPageNo(),queryParam.getPageSize(),totalCount);
        }
        List<Blog> blogs = blogService.selectClass(queryParam);
        return PageResponse.success(BlogConvertUtils.convert(blogs),queryParam.getPageNo(),queryParam.getPageSize(),totalCount);

    }

    @Override
    public List<BlogBaseInfo> queryRecommend(Long homeTypeId, int limit) {
        return blogService.queryRecommend(homeTypeId,limit);
    }

    @Override
    public List<BlogBaseInfo> selectTopN(String flag, int limit) {
        List<Blog> blogs =  blogService.selectTopN(flag,limit);
        return BlogConvertUtils.convertBlogs(blogs);
    }
}
